package com.bolt.auth.security.auth.server.component;

import com.alibaba.fastjson.JSON;
import com.bolt.common.web.RequestUtil;
import com.bolt.common.web.ResponseUtil;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.convention.data.code.CommonCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2020/11/23.
 */
@Service
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        if(RequestUtil.isAjaxRequest(httpServletRequest)){
            ResultMessage responseMessage =  Results.error(CommonCode.INVALID_AUTH,e);
            ResponseUtil.renderJson(httpServletResponse, JSON.toJSONString(responseMessage));
        }else{
            ResultMessage responseMessage =  Results.error(CommonCode.METHOD_NOT_ALLOWED,e);
            ResponseUtil.renderJson(httpServletResponse, JSON.toJSONString(responseMessage));
        }
    }
}

 